/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.assembler;

import com.inspur.edp.bff.api.manager.IFSManagerContext;
import com.inspur.edp.bff.api.manager.assembler.IExtendSaveAssembler;
import com.inspur.edp.bff.api.manager.context.SaveContext;
import com.inspur.edp.bff.spi.action.save.AfterSaveAction;
import com.inspur.edp.bff.spi.action.save.BeforeSaveAction;
import com.inspur.edp.bff.spi.action.save.DefaultSaveAction;
import com.inspur.edp.bff.spi.action.save.SaveAction;

import java.util.ArrayList;

public abstract class AbstractExtendSaveAssembler implements IExtendSaveAssembler {

    protected abstract ArrayList<BeforeSaveAction> getBeforeSaveActions(SaveContext saveContext);

    protected abstract ArrayList<AfterSaveAction> getAfterSaveActions(SaveContext saveContext);

    protected ArrayList<SaveAction> getSaveActions(SaveContext saveContext) {
        return null;
    }

    public final void doSave(IFSManagerContext context, SaveContext saveContext) {
        ArrayList<SaveAction> actions = getSaveActions(saveContext);
        if (actions == null || actions.isEmpty()) {
            DefaultSaveAction detaultAction = new DefaultSaveAction(saveContext);
            detaultAction.deal(context);
        } else {
            for (SaveAction action : actions) {
                action.deal(context);
            }
        }
    }

    public final void doBeforeSave(IFSManagerContext context, SaveContext saveContext) {
        ArrayList<BeforeSaveAction> actions = getBeforeSaveActions(saveContext);
        if (actions == null || actions.size() < 1) {
            return;
        }

        for (BeforeSaveAction action : actions) {
            action.deal(context);
        }
    }

    public final void doAfterSave(IFSManagerContext context, SaveContext saveContext) {
        ArrayList<AfterSaveAction> actions = getAfterSaveActions(saveContext);
        if (actions == null || actions.size() < 1) {
            return;
        }

        for (AfterSaveAction action : actions) {
            action.deal(context);
        }
    }
}
